<?php

namespace app\index\controller;

use app\common\controller\Frontend;

/**
 * Cron监控数据
 * @internal
 */
class Cron extends Frontend
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
    }

    public function auto(){
        if (!config('site.auto_key') || input('get.key') !== config('site.auto_key')) exit('key is not ok!');
        $time = date("Y-m-d").' 00:00:00';
        $res = $this->CommonWithdrawAutoModel->where('createtime','>= time',$time)->select();
        if ($res) exit('batch list is already created');
        $res = $this->CommonUserModel->where(['status'=>'normal'])->select();
        $user = [];
        $withdraw = is_numeric(config('site.withdraw_need')) ? config('site.withdraw_need') : 10;
        foreach ($res as $k => $v) {
            if ($v['money'].'' >= $withdraw.'' && $v['withdraw_account'] && $v['withdraw_name'] && $v['withdraw_type']) {
                if ($v['api_id'].'' !== config('site.pay_id').'') {
                    $user[] = $v;
                }
            }
        }
        if (!$user) exit('no user');
        $mark = date("Ymd").rand(111,999);
        $p = is_numeric(config("site.withdraw_money")) ? config('site.withdraw_money') : 1;
        $money = 0;
        foreach ($user as $k => $v) {
            if ($v['money'] <= round(config('site.withdraw_x'),2)) {
                $p2 = config('site.withdraw_x_min_money');
            }else{
                $p2 = round($v['money']*$p/100,2);
                if ($p2 > config('site.withdraw_x_max_money')) {
                    $p2 = config('site.withdraw_x_max_money');
                }
                if ($p2 < config('site.withdraw_x_min_money')) {
                    $p2 = config('site.withdraw_x_min_money');
                }
            }
            $realmoney = round($v['money']-$p2,2);
            if ($realmoney<=0) continue;
            $this->CommonUserModel->where(['id'=>$v['id']])->update(['money'=>0]);
            $this->CommonWithdrawModel->insert([
                'uid'=>$v['id'],
                'type'=>0,
                'mark'=>$mark,
                'withdraw_name'=>$v['withdraw_name'],
                'withdraw_type'=>$v['withdraw_type'],
                'withdraw_account'=>$v['withdraw_account'],
                'money'=>$v['money'],
                'realmoney'=>$realmoney,
                'percent'=>$p2,
                'status'=>0,
                'back'=>'',
                'createtime'=>time(),
            ]);
            $money = round($v['money']+$money,2);
        }
        $this->CommonWithdrawAutoModel->insert([
            'mark'=>$mark,
            'money'=>$money,
            'createtime'=>time(),
            'status'=>0,
            'config'=>'',
        ]);
        // 删除前天前未支付的订单
        $time2 = date("Y-m-d",strtotime("-1 day")).' 23:59:59';
        $time1 = date("Y-m-d",strtotime("-3 day")).' 23:59:59';
        $this->CommonPayOrderModel->where(['status'=>0])->where('createtime','between time',[$time1,$time2])->delete();
        // ok
        exit('ok');
    }

    public function order(){
        if (!config('site.auto_key') || input('get.key') !== config('site.auto_key')) exit('key is not ok!');
        $time2 = date("Y-m-d H:i:s",strtotime("-2 Minute"));
        $time1 = date("Y-m-d H:i:s",strtotime("-5 Minute"));
        $list = $this->CommonPayOrderModel->where(['status'=>0,'type_b'=>'easy'])->where('createtime','between time',[$time1,$time2])->select();
        if (!$list) exit('no order');
        $count = 0;
        foreach ($list as $k => $v) {
            $config = unserialize($v['config']);
            if (is_array($config) && isset($config['id']) && isset($config['key']) && isset($config['apiurl'])) {
                if ($config['apiurl'] !== 'http://pay.azfpay.com/') {
                    $url = $config['apiurl'].'api.php?act=order&pid='.$config['id'].'&key='.$config['key'].'&out_trade_no='.$v['trade_no'];
                    $curl = getCurl(['url'=>$url,'type'=>'get']);
                    $json = json_decode($curl,true);
                    if (isset($json['code']) && $json['code'].'' == '1') {
                        if ($json['out_trade_no'] == $v['trade_no'] && $json['type'] == $v['type'] && $json['status'].'' == '1') {
                            $config['check'] = true;
                            $this->CommonPayOrderModel->where(['id'=>$v['id']])->update(['config'=>serialize($config)]);
                            orderEnd($v['id']);
                            $count++;
                        }
                    }
                }
            }
        }
        exit($count.'-num-ok');
    }
}